package com.ocom.aiot.controller;


import com.github.pagehelper.PageInfo;
import com.ocom.aiot.service.AppVerInfoService;
import com.ocom.aiot.vo.consumedevice.AddDeviceVerInfoVo;
import com.ocom.aiot.vo.consumedevice.GetDeviceVerInfoVo;
import com.ocom.common.constant.AiotUrl;
import com.ocom.common.entity.aiot.EntityXfAppVerInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class DeviceVerInfoController {
    private final Logger log = LoggerFactory.getLogger(DeviceVerInfoController.class);

    @Autowired
    private AppVerInfoService appVerInfoService;

    // 2.4.1.保存设备升级版本信息（addDeviceVerInfo）
    @PostMapping(AiotUrl.URL_Aiot_AddDeviceVerInfo)
    public Result AddDeviceVerInfo(@Validated @RequestBody AddDeviceVerInfoVo addDeviceVerInfoVo){
        if (addDeviceVerInfoVo.getApkType() == null || addDeviceVerInfoVo.getAppVer() == null|| addDeviceVerInfoVo.getApkFileUrl().isEmpty()){
            log.error("版本类型为空"+addDeviceVerInfoVo.getApkType());
            log.error("版本为空"+addDeviceVerInfoVo.getAppVer());
            log.error("文件路径为空"+addDeviceVerInfoVo.getApkFileUrl());
            return ResultUtil.error(ResultEnum.Err_203);
        }
        try {
            appVerInfoService.AddDeviceVerInfo(addDeviceVerInfoVo);
        }catch (Exception exception){
            log.error("保存升级版本异常："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009);
        }

        return ResultUtil.success();
    }

    // 2.4.2.获取设备升级版本列表（getDeviceVerInfo）
    @PostMapping(AiotUrl.URL_Aiot_GetDeviceVerInfo)
    public Result GetDeviceVerInfo(@Validated @RequestBody GetDeviceVerInfoVo getDeviceVerInfoVo){
        Map<String, Object> map = new HashMap<>();
        try {
            List<EntityXfAppVerInfo> entityXfAppVerInfos = appVerInfoService.GetDeviceVerInfo(getDeviceVerInfoVo);
            PageInfo<EntityXfAppVerInfo> entityAppVerInfoPageInfo = new PageInfo<>(entityXfAppVerInfos);
            map.put("total",entityAppVerInfoPageInfo.getTotal());
            map.put("list", entityXfAppVerInfos);
        }catch (Exception exception){
            log.error("获取升级版本异常："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009);
        }

        return ResultUtil.success(map);
    }

    //2.4.3.删除设备升级版本（deleteDeviceVerInfo）
    @PostMapping(AiotUrl.URL_Aiot_DeleteDeviceVerInfo)
    public Result DeleteDeviceVerInfo(@RequestParam("id") Integer id){
        if (id == null){
            log.error("必填参数为空: "+id);
            return ResultUtil.error(ResultEnum.Err_203,"id: "+id);
        }
        try {
            appVerInfoService.DeleteDeviceVerInfo(id);
        }catch (Exception exception){
            log.error("删除升级版本异常："+exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009);
        }

        return ResultUtil.success();
    }





}

